
#include "CBondSource.h"

#include "../BondContainer.h"
#include "../SSVContainer.h"
#include "CSharedResource.h"

CBondSourceAvailable::CBondSourceAvailable(const BLHCONDITION& blh)
    : m_array_result(0),
      m_blh(blh),
      m_position(0),
      m_searchType(CBondContainer::typeNone) {}

BondID CBondSourceAvailable::GetFirstBondID() {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
  // AfxGetBondContainer().RetrieveFromStatic(m_blh, m_array_result);
  CSSVContainer& ssvContain = CSSVContainer::availableBond();
  ssvContain.ExtractWithSearchString(*m_array_result, m_blh, m_searchType,
                                     m_searchString.c_str(), true);
  return GetNextBondID();
}

CBondSourceAvailable::~CBondSourceAvailable() {
  if (m_array_result) {
    m_array_result->clear();
    CSharedResourceManager<BondIDVec>::Get().Free(m_array_result);
  }
}

BondID CBondSourceAvailable::GetNextBondID() {
  BondID bid = (m_position >= 0 && m_position < (int)m_array_result->size())
                   ? (*m_array_result)[m_position++]
                   : -1;
  return bid;
}

void CBondSourceAvailable::SetSearch(int nType, const char* szSearch) {
  m_searchType = nType;
  if (szSearch) m_searchString = szSearch;
}

////////////////////////////////////////////////////////////////////////////////
BondID CBondSourceAvailableABSBond::GetFirstBondID() {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
  CSSVContainer& ssvContain = CSSVContainer::availableAbsBond();
  // TODO:
  // ssvContain.ABSRetrieve(m_blh, *m_array_result, true);
  return GetNextBondID();
}

////////////////////////////////////////////////////////////////////////////////
CBondSourceStaticABSBond::CBondSourceStaticABSBond(int type, const char* szType)
    : CBondSourceStatic(type, szType) {}

BondID CBondSourceStaticABSBond::GetFirstBondID() {
  CSSVContainer::availableAbsBond().RetrieveFromStatic(m_type, m_search.c_str(),
                                                       *m_array_result);
  return GetNextBondID();
}

////////////////////////////////////////////////////////////////////////////////
BondID CBondSourceCFETSAvailable::GetFirstBondID() {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
  // AfxGetBondContainer().RetrieveFromStatic(m_blh, m_array_result);
  CSSVContainer& ssvContain = CSSVContainer::availableBond();
  // TODO:
  // ssvContain.CFETSRetrieveWithSearchString(m_blh, *m_array_result,
  // m_searchType, m_searchString.c_str(), true);
  return GetNextBondID();
}

////////////////////////////////////////////////////////////////////////////////
BondID CBondSourceCFETSDealAvailable::GetFirstBondID() {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
  CSSVContainer& ssvContain = CSSVContainer::availableBond();
  // TODO:
  // ssvContain.CFETSRetrieve(m_blh, *m_array_result, true);
  return GetNextBondID();
}

////////////////////////////////////////////////////////////////////////////////
CBondSourceStatic::CBondSourceStatic(int type, const char* szType)
    : m_type(type), m_search(szType ? szType : ""), m_position(0) {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
}
CBondSourceStatic::~CBondSourceStatic() {
  m_array_result->clear();
  CSharedResourceManager<BondIDVec>::Get().Free(m_array_result);
}
BondID CBondSourceStatic::GetFirstBondID() {
  CBondContainer::instance().RetrieveFromStatic(m_type, m_search.c_str(),
                                                *m_array_result);
  return GetNextBondID();
}

BondID CBondSourceStatic::GetNextBondID() {
  return (m_position >= 0 && m_position < (int)m_array_result->size())
             ? (*m_array_result)[m_position++]
             : -1;
}

////////////////////////////////////////////////////////////////////////////////
CBondCFETSSourceStatic::CBondCFETSSourceStatic(int type, const char* szType)
    : CBondSourceStatic(type, szType) {}

BondID CBondCFETSSourceStatic::GetFirstBondID() {
  CBondContainer::instance().RetrieveFromStaticCFETS(m_type, m_search.c_str(),
                                                     *m_array_result);
  return GetNextBondID();
}
////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////
CBondSSEFISourceStatic::CBondSSEFISourceStatic(int type, const char* szType)
    : CBondSourceStatic(type, szType) {}

BondID CBondSSEFISourceStatic::GetFirstBondID() {
  CBondContainer::instance().RetrieveFromStaticSSEFI(m_type, m_search.c_str(),
                                                     *m_array_result);
  return GetNextBondID();
}

////////////////////////////////////////////////////////////////////////////////
BondID CBondSourceSSEFIAvailable::GetFirstBondID() {
  m_array_result = CSharedResourceManager<BondIDVec>::Get().Alloc();
  m_array_result->clear();
  // AfxGetBondContainer().RetrieveFromStatic(m_blh, m_array_result);
  CSSVContainer& ssvContain = CSSVContainer::availableBond();
  // TODO
  // ssvContain.SSEFIRetrieve(m_blh, *m_array_result);
  return GetNextBondID();
}
